home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1998 June / Macworld (1998-06).dmg / Shareware World / Info / For Developers / MacsBug 6.5.4a4 / Building dcmds / C Samples / Echo.c < prev    next >
Text File  |  1998-02-11  |  3KB  |  128 lines

  1. /*
  2.     File:        Echo.c
  3.  
  4.     Contains:    A sample dcmd which echos all command line parameters.
  5.  
  6.     Written by:    JM3 = Jim Murphy
  7.                 DAL = Dave Lyons
  8.  
  9.     Copyright:    © 1988, 1994, 1996 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.     Change History (most recent first):
  12.  
  13.          <5>     9/23/96    DAL        Honor the "aborted" field in the parameter block, and handle
  14.                                     quoted strings correctly. It's still goofy if you do "echo 0/0",
  15.                                     though (the "if (ok)...else") is not working as expected.
  16.          <4>   25-Jan-96    JM3        Added sample build commands.
  17.          <3>   10-Dec-94    JM3        Updated for new format 3 dcmd requirements.
  18.          <2>    1-Sep-94    JM3        Included string.h so we build with no warnings with -r.
  19.  
  20.     The following MPW commands will build the dcmd and copy it to the "Debugger Prefs" file
  21.     in the System folder. The dcmd's name in MacsBug will be the name of the file built by
  22.     the Linker.
  23.  
  24.     C Echo.c
  25.     Link -o Echo -sg Main=STDCLIB,STDIO,SANELIB dcmdGlue.a.o Echo.c.o"    ∂
  26.         "{Libraries}Runtime.o" "{CLibraries}StdCLib.o"
  27.     BuildDcmd Echo 192 -format3
  28.     Echo 'include "Echo";'    |    Rez -a -o "{SystemFolder}Debugger Prefs"
  29.  
  30. */
  31.  
  32.  
  33. #include <Memory.h>
  34. #include <Types.h>
  35. #include <string.h>
  36.  
  37. #include "dcmd.h"
  38.  
  39. void NumberToHex(long number, Str255 hex)
  40. {
  41.  
  42.     Str255    digits = "0123456789ABCDEF";
  43.     int        n;
  44.  
  45.     strcpy(hex, &".00000000");
  46.     hex[0] = 8;
  47.     for (n = 8; n >= 1; n--)
  48.     {
  49.         hex[n] = digits[number % 16];
  50.         number = number / 16;
  51.     }
  52.  
  53. } // NumberToHex
  54.  
  55.  
  56. pascal void CommandEntry(dcmdBlock* paramPtr)
  57. {
  58.  
  59.     short    pos, ch;
  60.     long    value;
  61.     Boolean    ok;
  62.     Str255    str;
  63.  
  64.     static const char usageStr[] = "\p[params...]";
  65.  
  66.     switch (paramPtr->request)
  67.     {
  68.         case dcmdInit:
  69.             break;
  70.  
  71.         case dcmdHelp:
  72.             dcmdDrawLine("\pEcho the command line parameters");
  73.             break;
  74.  
  75.         case dcmdGetInfo:
  76.             * (long *) &((GetInfoRequestBlockPtr) paramPtr->requestIOBlock)->dcmdVersion = 0x03008000; // version 3.0 final
  77.             BlockMoveData(&usageStr, &((GetInfoRequestBlockPtr) paramPtr->requestIOBlock)->usageStr, usageStr[0]+1);
  78.             break;
  79.  
  80.         case dcmdDoIt:
  81.             do
  82.             {
  83.                 // Save the position so we can rewind if we get an error
  84.             
  85.                 pos = dcmdGetPosition();
  86.                 ch  = dcmdPeekAtNextChar();
  87.  
  88.                 if (((ch >= 'A') && (ch <= 'Z')) || ((ch >= 'a') && (ch <= 'z')) || (ch == '"') || (ch == '\''))
  89.                 {
  90.                     // Get the parameter as a text string
  91.  
  92.                     ch = dcmdGetNextParameter(str);
  93.                     dcmdDrawLine(str);
  94.  
  95.                 }
  96.                 else
  97.                 {
  98.                     // Get the parameter as a 32 bit value
  99.  
  100.                     ch = dcmdGetNextExpression(&value, &ok);
  101.  
  102.                     if (ok)
  103.                     {    
  104.                         // The expression was parsed correctly
  105.  
  106.                         NumberToHex(value, str);
  107.                         dcmdDrawLine(str);
  108.  
  109.                     }
  110.                     else
  111.                     {    
  112.                         // The expression contained an error. Get it as a string
  113.  
  114.                         dcmdSetPosition(pos);
  115.                         ch = dcmdGetNextParameter(str);
  116.                         dcmdDrawLine(str);
  117.                     }
  118.                 }
  119.             } while (ch != '\n' && !paramPtr->aborted);
  120.             break;
  121.  
  122.         // Version 3 and newer dcmds must quietly ignore requests we don't recognize.
  123.     
  124.         default:
  125.             break;
  126.     }
  127. } // CommandEntry
  128.